package com.gzy.literature.controller;

import com.github.pagehelper.Page;
import com.gzy.literature.entity.UserWorks;
import com.gzy.literature.service.UserWorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userWorks")
public class UserWorksController {

    @Autowired
    private UserWorksService userWorksService;

    @GetMapping("/{userId}/{workId}")
    public ResponseEntity<UserWorks> getUserWorksByIdAndWorkId(@PathVariable Integer userId, @PathVariable Integer workId) {
        UserWorks userWorks = userWorksService.queryByIdAndWorkId(userId, workId);
        if (userWorks == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userWorks);
    }

    @GetMapping("/page")
    public ResponseEntity<Page<UserWorks>> getUserWorksByPage(
            @RequestBody(required = false) UserWorks userWorks,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by("userId").ascending());
        // 注意：这里需要调整以实际支持分页查询。当前的queryAllByCondition方法不支持分页。
        // 理想情况下，你应该在UserWorksDao中添加一个支持分页的查询方法。
        List<UserWorks> userWorksList = userWorksService.queryFll(userWorks, pageNum, pageSize).getList(); // 假设已修改service以返回正确分页结果
        long total = userWorksService.queryFll(userWorks, 1, 1).getTotal(); // 这里是一个简化的计算total的方式，需要改进

        Page<UserWorks> page = new Page<>();

        return ResponseEntity.ok(page);
    }

    @PostMapping
    public ResponseEntity<UserWorks> createUserWorks(@RequestBody UserWorks userWorks) {
        UserWorks createdUserWorks = userWorksService.insert(userWorks);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdUserWorks);
    }

    @PutMapping
    public ResponseEntity<Integer> updateUserWorks(@RequestBody UserWorks userWorks) {
        int updatedRows = userWorksService.update(userWorks);
        return ResponseEntity.ok(updatedRows);
    }

    @DeleteMapping("/{userId}")
    public ResponseEntity<Integer> deleteUserWorksByUserId(@PathVariable Integer userId) {
        int deletedRows = userWorksService.deleteByUserId(userId);
        return ResponseEntity.ok(deletedRows);
    }

    @DeleteMapping("/{userId}/{workId}")
    public ResponseEntity<Integer> deleteUserWorksByIdAndWorkId(@PathVariable Integer userId, @PathVariable Integer workId) {
        int deletedRows = userWorksService.deleteByIdAndWorkId(userId, workId);
        return ResponseEntity.ok(deletedRows);
    }

    // 注意：上面的getUserWorksByPage方法中的分页处理是简化的，并不正确。
    // 正确的做法是在UserWorksDao中添加一个支持分页的查询方法，并在UserWorksServiceImpl中调用它。
    // 例如，可以在UserWorksDao中添加如下方法：
    // @Select("<script>" + "SELECT * FROM user_works WHERE 1=1 " + "<if test='userWorks.userId != null'> AND user_id = #{userWorks.userId} </if>" + " ORDER BY ${sortField} ${sortOrder}" + "</script>")
    // @Results(id = "userWorksResultMapList", value = { @Result(property = "userId", column = "user_id"), @Result(property = "workId", column = "work_id") })
    // List<UserWorks> queryAllByConditionAndSort(@Param("userWorks") UserWorks userWorks, @Param("sortField") String sortField, @Param("sortOrder") String sortOrder, @Param("pageable") Pageable pageable);
    // 然后在UserWorksServiceImpl的queryByPage方法中使用这个新的查询方法。
}